package com.masterclient.app.businessmodule.usermodule.login.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.App;
import com.masterclient.app.R;
import com.masterclient.app.base.ConfirmDialog;
import com.masterclient.app.businessmodule.usermodule.login.beans.HeadUserInfoModel;
import com.masterclient.app.businessmodule.usermodule.login.beans.InviteInfoData;
import com.masterclient.app.businessmodule.usermodule.login.beans.InviterSelectPopModel;
import com.masterclient.app.businessmodule.usermodule.login.beans.InviterUserInfoPopModel;
import com.masterclient.app.businessmodule.usermodule.login.beans.LoginSuccessData;
import com.masterclient.app.widget.CircleTransform;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：hxm
 * 创建时间：2020/7/16
 * 功能说明：
 */

public class InviteDialog extends Dialog {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_invite)
    TextView tvInvite;
    @BindView(R.id.shareer_head)
    ImageView sharperHead;
    @BindView(R.id.shareer_name)
    TextView sharperName;
    @BindView(R.id.invite_question)
    TextView inviteQuestion;
    @BindView(R.id.allocate_reason)
    TextView allocateReason;
    @BindView(R.id.input_invite_code)
    EditText inputInviteCode;
    @BindView(R.id.tv_error_tip)
    TextView tvErrorTip;
    @BindView(R.id.gzg_layout)
    View gzgLayout;
    @BindView(R.id.inviter_layout)
    View inviterLayout;
    @BindView(R.id.input_inviter_code_layout)
    View inputInviterCodeLayout;
    @BindView(R.id.title_layout)
    View titleLayout;
    @BindView(R.id.bottom_layout)
    View bottomLayout;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private OnChooseListener onChooseListener;
    private Context context;
    private boolean hasInputInviteCode = false;
    private ConfirmDialog confirmDialog;
    private String popType;
    private String inviteCode = "";
    private LoginSuccessData loginSuccessData;

    public void setOnChooseListener(OnChooseListener onChooseListerner) {
        this.onChooseListener = onChooseListerner;
    }

    public InviteDialog(@NonNull Context context, OnChooseListener onChooseListener) {
        super(context, R.style.MyDialog);
        this.onChooseListener = onChooseListener;
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_invite);
        ButterKnife.bind(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
        windowParams.width = ScreenUtils.getScreenWidth();
        window.setGravity(Gravity.CENTER);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowParams);
    }

    /**
     * 设置邀请人弹窗信息
     * @param data 登录成功数据
     */
    public void setData(LoginSuccessData data) {
        loginSuccessData = data;
        titleLayout.setVisibility(View.GONE);
        gzgLayout.setVisibility(View.GONE);
        inviterLayout.setVisibility(View.GONE);
        inputInviterCodeLayout.setVisibility(View.GONE);
        mRecyclerView.setVisibility(View.GONE);
        bottomLayout.setVisibility(View.GONE);

        if (data == null || (data.getInviter_code_select_pop() == null && data.getInviter_user_info_pop() == null)) {
            Toast toast = Toast.makeText(context, "网络异常", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            dismiss();
            return;
        }

        if (data.isIs_show_user_info_pop() && data.getInviter_user_info_pop() != null) {
            //非选择弹窗
            popType = data.getInviter_user_info_pop().userPopType;
            InviterUserInfoPopModel inviterUserInfoPopModel = data.getInviter_user_info_pop();
            if(inviterUserInfoPopModel != null){
                inviteCode = inviterUserInfoPopModel.inviterCode;
            }
            HeadUserInfoModel headUserInfoModel = data.getHead_user_info();
            if ("4".equals(popType)) {
                //4、绑定邀请人弹窗
                titleLayout.setVisibility(View.VISIBLE);
                inviterLayout.setVisibility(View.VISIBLE);
                bottomLayout.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(headUserInfoModel.avatar)) {
                    App.picasso.load(headUserInfoModel.avatar).transform(new CircleTransform()).error(R.mipmap.user_head_default_icon0).placeholder(
                            R.mipmap.user_head_default_icon0).into(sharperHead);
                }
                sharperName.setText(headUserInfoModel.nickName+"邀请你加入中华大师汇");
                if (!TextUtils.isEmpty(inviterUserInfoPopModel.inviterAvatar)) {
                    App.picasso.load(inviterUserInfoPopModel.inviterAvatar).transform(new CircleTransform()).error(R.mipmap.user_head_default_icon0).placeholder(
                            R.mipmap.user_head_default_icon0).into(ivHead);
                }
                tvName.setText(inviterUserInfoPopModel.inviterNickname);
                tvInvite.setText(String.format("邀请码：%s", inviterUserInfoPopModel.inviterCode));
                tvTitle.setText("点击确认成为以下用户的粉丝");
                inviteQuestion.setVisibility(View.GONE);
                allocateReason.setVisibility(View.GONE);
            } else if ("2".equals(popType)) {
                //2、输入邀请码弹窗
                titleLayout.setVisibility(View.VISIBLE);
                inputInviterCodeLayout.setVisibility(View.VISIBLE);
                bottomLayout.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(headUserInfoModel.avatar)) {
                    App.picasso.load(headUserInfoModel.avatar).transform(new CircleTransform()).error(R.mipmap.user_head_default_icon0).placeholder(
                            R.mipmap.user_head_default_icon0).into(sharperHead);
                }
                sharperName.setText("感谢您加入中华大师汇");
                hasInputInviteCode = true;
            }
        } else if (data.isIs_show_code_select_pop() && data.getInviter_code_select_pop() != null) {
            //选择弹窗
            InviterSelectPopModel inviterSelectPopModel = data.getInviter_code_select_pop();
            InviterSelectPopModel.BindInviterCode bindInviterCode = inviterSelectPopModel.bindInviterCode;
            if(inviterSelectPopModel != null && inviterSelectPopModel.bindInviterCode != null){
                inviteCode = inviterSelectPopModel.bindInviterCode.inviterCode;
            }
            HeadUserInfoModel headUserInfoModel = data.getHead_user_info();

            titleLayout.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(headUserInfoModel.avatar)) {
                App.picasso.load(headUserInfoModel.avatar).transform(new CircleTransform()).error(R.mipmap.user_head_default_icon0).placeholder(
                        R.mipmap.user_head_default_icon0).into(sharperHead);
            }
            sharperName.setText(headUserInfoModel.nickName+"邀请您加入中华大师汇 您可以");
            mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
            InviteAdapter inviteAdapter = new InviteAdapter(type -> {
                popType = type;
                titleLayout.setVisibility(View.GONE);
                gzgLayout.setVisibility(View.GONE);
                inviterLayout.setVisibility(View.GONE);
                inputInviterCodeLayout.setVisibility(View.GONE);
                mRecyclerView.setVisibility(View.GONE);
                switch (popType) {
                    case "2":
                        //2： 输入邀请码
                        titleLayout.setVisibility(View.VISIBLE);
                        inputInviterCodeLayout.setVisibility(View.VISIBLE);
                        bottomLayout.setVisibility(View.VISIBLE);
                        if (!TextUtils.isEmpty(headUserInfoModel.avatar)) {
                            App.picasso.load(headUserInfoModel.avatar).transform(new CircleTransform()).error(R.mipmap.user_head_default_icon0).placeholder(
                                    R.mipmap.user_head_default_icon0).into(sharperHead);
                        }
                        sharperName.setText("感谢您加入中华大师汇");
                        hasInputInviteCode = true;
                        break;
                    case "3":
                        //3：成为平台粉丝
                        gzgLayout.setVisibility(View.VISIBLE);
                        bottomLayout.setVisibility(View.VISIBLE);
                        break;
                    case "4":
                    case "5":
                        //4：成为邀请码粉丝
                        //5：绑定给分配的人（自流）
                        inviterLayout.setVisibility(View.VISIBLE);
                        bottomLayout.setVisibility(View.VISIBLE);
                        if (!TextUtils.isEmpty(bindInviterCode.inviterAvatar)) {
                            App.picasso.load(bindInviterCode.inviterAvatar).transform(new CircleTransform()).error(R.mipmap.user_head_default_icon0).placeholder(
                                    R.mipmap.user_head_default_icon0).into(ivHead);
                        }
                        tvName.setText(bindInviterCode.inviterNickname);
                        tvInvite.setText(String.format("邀请码：%s", bindInviterCode.inviterCode));
                        tvTitle.setText("点击确认成为以下用户的粉丝");
                        inviteQuestion.setVisibility(View.VISIBLE);
                        allocateReason.setVisibility(View.GONE);
                        allocateReason.setText(inviterSelectPopModel.toastInfo);
                        break;
                    case "6":
                        //6：跳过绑定
                        onChooseListener.skip_login();
                    case "0":
                        //0：不登录
                        onChooseListener.cancel();
                        break;
                }
            });
            mRecyclerView.setAdapter(inviteAdapter);
            if (inviterSelectPopModel.selectType != null && inviterSelectPopModel.selectType.size() > 0) {
                inviteAdapter.setNewData(data.getInviter_code_select_pop().selectType);
            }
        }
    }

    /**
     * 刷新邀请人信息
     * @param inviteInfoData  邀请人信息
     * @param notice 文案
     */
   public void refreshInviteInfo(InviteInfoData inviteInfoData,String notice) {
        this.popType = "4";
        if (!TextUtils.isEmpty(notice)){
            titleLayout.setVisibility(View.VISIBLE);
            sharperHead.setVisibility(View.GONE);
            sharperName.setVisibility(View.VISIBLE);
            sharperName.setText(notice);
            inviteQuestion.setVisibility(View.VISIBLE);
        }else {
            inviteQuestion.setVisibility(View.GONE);
            titleLayout.setVisibility(View.GONE);
        }
        gzgLayout.setVisibility(View.GONE);
        inviterLayout.setVisibility(View.GONE);
        inputInviterCodeLayout.setVisibility(View.GONE);
        mRecyclerView.setVisibility(View.GONE);
        bottomLayout.setVisibility(View.GONE);

        if (inviteInfoData == null) {
            Toast toast = Toast.makeText(context, "网络异常", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            dismiss();
            return;
        }

        //4、绑定邀请人弹窗
        inviteCode = inviteInfoData.getInvite_code();
        inviterLayout.setVisibility(View.VISIBLE);
        bottomLayout.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(inviteInfoData.getImage())) {
            App.picasso.load(inviteInfoData.getImage()).transform(new CircleTransform()).error(R.mipmap.user_head_default_icon0).placeholder(
                    R.mipmap.user_head_default_icon0).into(ivHead);
        }
        tvName.setText(inviteInfoData.getNick_name());
        tvInvite.setText(String.format("邀请码：%s", inviteInfoData.getInvite_code()));
        tvTitle.setText("点击确认成为以下用户的粉丝");
        allocateReason.setVisibility(View.GONE);
    }

    /**
     * 刷新错误信息
     * @param msg 错误信息
     */
   public void refreshErrorInfo(String msg) {
        tvErrorTip.setText(msg);
        tvErrorTip.setVisibility(View.VISIBLE);
    }

    @OnClick({R.id.tv_cancel, R.id.tv_confirm, R.id.invite_question})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                onChooseListener.cancel();
                break;
            case R.id.tv_confirm:
                if ("2".equals(popType)) {
                    String invite_code = inputInviteCode.getText().toString().trim();
                    if (TextUtils.isEmpty(invite_code)) {
                        Toast toast = Toast.makeText(context, "请输入邀请码", Toast.LENGTH_LONG);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                        return;
                    }


                    onChooseListener.getByInviteCode(invite_code);
                } else {
                    onChooseListener.confirm(inviteCode);
                }
                break;
            case R.id.invite_question:
                if (allocateReason.getVisibility() == View.VISIBLE) {
                    allocateReason.setVisibility(View.GONE);
                } else {
                    allocateReason.setVisibility(View.VISIBLE);
                }
                break;
        }
    }

    private void showSecondConfirmDialog() {
        confirmDialog = new ConfirmDialog(context, "点击取消将无法登陆也无法绑定\n" + "确认取消吗？", "确认取消", "继续绑定");
        confirmDialog.setOnChooseListerner(new ConfirmDialog.OnChooseListerner() {
            @Override
            public void cancel() {
                confirmDialog.dismiss();
                if (hasInputInviteCode && inputInviterCodeLayout.getVisibility() != View.VISIBLE) {
                    setData(loginSuccessData);
                } else {
                    onChooseListener.cancel();
                }
            }

            @Override
            public void confirm() {
                confirmDialog.dismiss();
                confirmDialog = null;
            }
        });
        confirmDialog.show();
    }

    public interface OnChooseListener {
        void cancel();

        void getByInviteCode(String inviteCode);

        void confirm(String inviteCode);

        void skip_login();
    }
}
